package com.example.zg3_park.news

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.lib_base.entity.NewsEntityItem
import com.example.lib_base.net.ApiService

class NewsDataSource(val apiService: ApiService):PagingSource<Int,NewsEntityItem>() {
    override fun getRefreshKey(state: PagingState<Int, NewsEntityItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NewsEntityItem> {
        try {
            //当前
            val currentPage = params.key?:0
            //请求数据
            val news = apiService.getNews()
            //下一页
            val nextPage = if(news.data.size == 10) currentPage +1 else null

            return LoadResult.Page(news.data,null,nextPage)
        }catch (e:Exception) {
            return LoadResult.Error(e)
        }
    }
}